var app = getApp();
Page({
  data: {
    vertical: false,
    wheight: 0,
    order: {},//订单信息
    wid:'', 
    mc: '',//辅导班名称
    nj: '',//年级
    bj: '',//班级
    km: '',//科目
    fy: 0,//辅导班的费用
    sksj: '',//上课时间
    skdd: '',//上课地点
    bmzlsm: '',//报名资料说明
    shqk:0,//默认报名审核情况
    sfjzsj: '',//收费截止时间
    bmlb:'',//报名类别

    priarr: [],//被选中的收费
    teas: [],//教师列表
    allprice: 0,//总价格

    imgitems: [],//上传的图片
    bmqksm: '',//报名情况说明

    prepay_id: '',//交易id
    wxid: '',
    urls: [],//上传的图片

    fs1state: '',//付款方式1  支付到个人
    fs2state: '',//付款方式2 支付到银行账号
    fs3state: '',//微信支付

    fs1: '',//付款方式1  支付到个人
    fs2: '',//付款方式2 支付到银行账号
    fs3: '',//微信支付
  }, 
  cusImageLoad: function (e) {
    var that = this;
    that.setData(WxAutoImage.wxAutoImageCal(e));
  },



  onLoad: function (options) {
    var that = this;
   
    var openid = wx.getStorageSync('openid');
    if (openid == "") {
      wx.redirectTo({
        url: '../../pages/index/index'
      })
    } else {
      var chei=wx.getSystemInfoSync().windowHeight;
      var wheight = chei-90;

      that.setData({
        wxid: openid,
        wheight: wheight
      })
      that.getdata();
      that.getJffs();
      that.getsysteminfo();
    }
  },

  getdata: function () {//定义函数名称
    //获取传递的参数
    var pages = getCurrentPages();
    var prevPage = pages[pages.length - 2];  //上一个页面
    var wid = prevPage.data.wid;
    //辅导班名称
    var mc = prevPage.data.mc;
    //年级
    var nj = prevPage.data.nj;
    //班级
    var bj = prevPage.data.bj;
    //科目
    var km = prevPage.data.km;
    //辅导班的费用 
    var fy = prevPage.data.fy;
    //被选中的收费
    var priarr = prevPage.data.priarr;
    //教师列表
    var teas = prevPage.data.teas;
    //总价格
    var allprice = prevPage.data.allprice;
    //收费截止时间
    var sfjzsj = prevPage.data.sfjzsj;
    //报名类别
    var bmlb = prevPage.data.bmlb;

    var sksj = prevPage.data.sksj;//上课时间
    var skdd = prevPage.data.skdd;//上课地点
    var bmzlsm = prevPage.data.bmzlsm;//报名资料说明

    this.setData({
      wid: wid,
      mc: mc,
      nj: nj,
      bj: bj,
      km: km,
      fy: fy,
      priarr: priarr,
      teas: teas,
      allprice: allprice,
      sfjzsj: sfjzsj,
      sksj: sksj,
      skdd: skdd,
      bmzlsm: bmzlsm,
      bmlb: bmlb
    })
  },


  getsysteminfo: function () {
    var that = this;
    var csid = wx.getStorageSync('csid');
    wx.request({
      url: app.myUlife.ulifeUrl + '/getsystemSet.html',//请求地址
      data: {
        csid: csid
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        if (that.data.bmlb=='0'){
          if (res.data.WHKSH) {
            that.setData({
              shqk: res.data.WHKSH
            });
          } 
        }else{
          if (res.data.OTHERSH) {
            that.setData({
              shqk: res.data.OTHERSH
            });
          } 
        }     
      }
    })
  },
 
  //上传图片
  uploadimage: function () {
    var that = this;
    wx.chooseImage({
      count: 6, //张数， 默认9
      sizeType: ['compressed'], //建议压缩图
      sourceType: ['album', 'camera'], // 来源是相册、相机
      success: function (res) {
        var imgitems = that.data.imgitems;
        for (var i = 0; i < res.tempFilePaths.length; i++) {
          wx.showLoading({
            title: '上传中',
          })
          var img = {
            imgurl: res.tempFilePaths[i],
            height: 100
          };
          imgitems.push(img);
          that.uploadcommenturl(res.tempFilePaths[i]);
        }

        that.setData({
          imgitems: imgitems
        });
      }
    });
  },



  //上传图片到服务器
  uploadcommenturl: function (url) {
    var that = this;
    const uploadTask = wx.uploadFile({
      url: app.myUlife.ulifeUrl + '/wechart/uploadBmurl.html', 
      filePath: url,
      name: 'file',
      formData: {},
      success: function (res) {
        var img = JSON.parse(res.data);
        var imgurl = img.urls;
        var url1 = {
          imgurl: imgurl
        };
        var urls = that.data.urls;
        urls.push(url1);
        that.setData({
          urls: urls
        });
      }
    })
    uploadTask.onProgressUpdate((res) => {
      if (res.progress == 100) {
        wx.hideLoading()
      }
    })
  },

  //获取图片高度
  getheight: function (e) {
    var that = this;
    var heigh = e.detail.height;
    var width = e.detail.width;
    var windowWidth = that.data.windowWidth;
    var nwidth = windowWidth * 0.25;//现宽度
    var nheight = (nwidth * heigh) / width;
    nheight = nheight.toFixed(2);
    if (parseFloat(nheight) >= 100) {
      nheight = 100;
    }
    var index = e.currentTarget.dataset.index;
    var key = "imgitems[" + index + "].height";
    var obj = {};
    obj[key] = nheight;
    this.setData(obj);
  },


  //预览图片
  viewimage: function (e) {
    var cuindex = e.target.dataset.index;
    var that = this;
    var urls = [];
    var imgitems = that.data.imgitems;
    for (var i = 0; i < imgitems.length; i++) {
      urls.push(imgitems[i].imgurl);
    }
    wx.previewImage({
      current: imgitems[cuindex].imgurl, // 当前显示图片的http链接
      urls: urls // 需要预览的图片http链接列表
    })
  }, 


  //报名情况说明
  changinput: function (e) {
    this.setData({
      bmqksm: e.detail.value
    })
  },


  /** 
   * 购买商品
   */
  onsurepay: function (e) {
    var that = this;
    //学生id
    var sid = wx.getStorageSync('sid');
    //文化课id
    var wid = that.data.wid;
    //总价格 
    var allprice = that.data.allprice;
    //收费项目
    var priarr=that.data.priarr;
   
     //报名情况统计
    var bmqksm = that.data.bmqksm;
     //图片
    var urls = that.data.urls;
    //默认审核情况
    var shqk = that.data.shqk;
    //手续费
    var fs3 = that.data.fs3;
    wx.request({
      url: app.myUlife.ulifeUrl + 'wechart/insertOrder.html',
      data: {
        sid: sid,
        wid: wid,
        allprice: allprice,
        priarr: priarr,
        bmqksm: bmqksm,
        urls: urls,
        shqk: shqk,//默认报名审核情况
        fs3: fs3,//手续费
      },
      header: {
        "Content-Type": "applciation/json"
      },
      method: "get",//get为默认方法/POST
      success: function (res) {
        if (!res.data.result){
          wx.showModal({
            title: '友情提示',
            content: res.data.msg,
            showCancel: !1,
          })
        }else{
         
          if (that.data.shqk==0){
            wx.showModal({
              title: '友情提示',
              content: "报名成功,请等待审核,您可在[个人中心-我报名的辅导班里面查看并缴费]",
              showCancel: !1,
              success(res) {
                wx.redirectTo({
                  url: '../../pages/order/list/index'
                })
              }
            }) 
          }else{
            if (that.data.fs3state == 0) {
              that.setData({
                oid: res.data.oid
              })
              //需要提示用户 是选择线上支付还是线下支付
              if (that.data.fs1state == 0 || that.data.fs2state == 0) {
                wx.showModal({
                  title: '提示',
                  content: '如为线下付款,点击取消,如为线上付款,点击确定',
                  success(res) {
                    if (res.confirm) {
                      that.gotoPay();
                    } else if (res.cancel) {
                      that.jftip();
                    }
                  }
                })
              } else {
                that.gotoPay();
              }
            } else {
              that.jftip();
            }
          }
        }
      },
      fail: function (err) { },//请求失败
      complete: function () { }//请求完成后执行的函数
    })
  },



  getJffs: function () {//定义函数名称
    var that = this;
    var csid = wx.getStorageSync('csid');
    wx.request({
      url: app.myUlife.ulifeUrl + '/jffs/getJffsBycsid.html',//请求地址
      data: {//发送给后台的数据
        csid: csid
      },
      header: {//请求头
        "Content-Type": "applciation/json"
      },
      method: "GET",//get为默认方法/POST
      success: function (res) {
        if (res.data.FS1STATE) {
          that.setData({
            fs1state: res.data.FS1STATE,
          });
        }
        if (res.data.FS2STATE) {
          that.setData({
            fs2state: res.data.FS2STATE,
          });
        }
        if (res.data.FS3STATE) {
          that.setData({
            fs3state: res.data.FS3STATE,
          });
        }
        if (res.data.FS1) {
          that.setData({
            fs1: res.data.FS1,
          });
        }
        if (res.data.FS2) {
          that.setData({
            fs2: res.data.FS2,
          });
        }
        if (res.data.FS3) {
          that.setData({
            fs3: res.data.FS3,
          });
        }

      },
      fail: function (err) { },//请求失败
      complete: function () { }//请求完成后执行的函数
    })
  },


  gotoPay: function () {
    var that=this;
    wx.request({
      url: app.myUlife.ulifeUrl + '/wechart/submitorder.html',
      data: {
        oid: that.data.oid,
        ordername: that.data.mc,
        wxid: that.data.wxid,
        allprice: that.data.allprice,
        fs3: that.data.fs3,//手续费
      },
      header: {
        "Content-Type": "applciation/json"
      },
      method: "get",//get为默认方法/POST
      success: function (res) {
        var timeStamp = res.data.timeStamp;
        var nonceStr = res.data.nonceStr;
        var prepay_id = res.data.prepay_id;
        that.setData({
          prepay_id: prepay_id
        })
        var sign = res.data.sign;
        wx.requestPayment(
          {
            'timeStamp': timeStamp,
            'nonceStr': nonceStr,
            'package': 'prepay_id=' + prepay_id,
            'signType': 'MD5',
            'paySign': sign,
            'success': function (res) {
              console.info("支付成功");
              //修改订单为已经支付
              that.oradePay();
            },
            'fail': function (res) { },
            'complete': function (res) {
              console.info("支付完成");

            }
          })
      },
      fail: function (err) { },//请求失败
      complete: function () { }//请求完成后执行的函数
    })
  },


  oradePay: function (e) {
    var that = this;
    var oid = that.data.oid;//订单id
    if (oid == "") {
      return;
    }
    var prepay_id = that.data.prepay_id;
    if (prepay_id == "") {
      return;
    }
    wx.request({
      url: app.myUlife.ulifeUrl + '/wechart/paysuccess.html',
      data: {
        oid: oid,
        prepay_id: prepay_id
      },
      header: {
        "Content-Type": "applciation/json"
      },
      method: "get",//get为默认方法/POST
      success: function (res) {
        //跳转到成功页面
        wx.reLaunch({
          url: '../../pages/paysuccess/paysuccess?oid=' + oid
        })
      },
      fail: function (err) { },//请求失败
      complete: function () { }//请求完成后执行的函数
    })
  },


  jftip: function () {
    var that=this;
    var msg = "报名成功";
    if (that.data.fs1state == 0) {
      if (that.data.fs1 != null && that.data.fs1 != '') {
        msg += ",如现金支付,请联系(" + that.data.fs1 + ")";
      }
    }
    if (that.data.fs2state == 0) {
      if (that.data.fs2 != null && that.data.fs2 != '') {
        msg += ",如汇款至账户,账户为(" + that.data.fs2 + "),支付成功后,请向学校出示汇款证明";
      }
    }

    wx.showModal({
      title: '友情提示',
      content: msg,
      showCancel: !1,
      success(res) {
        wx.redirectTo({
          url: '../../pages/order/list/index'
        })
      }
    })
  }

})
